package pw.yumc.Residence.commandsub;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.bekvon.bukkit.residence.permissions.PermissionGroup;
import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.ResidenceManager;

import cn.citycraft.PluginHelper.commands.BaseCommand;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.text.Language;

public class CommandKick extends BaseCommand {
    ResidenceMain plugin;

    public CommandKick(final ResidenceMain plugin) {
        super("k");
        this.plugin = plugin;
        setMinimumArguments(1);
        setOnlyPlayerExecutable();
        setPossibleArguments("<目标玩家>");
        setDescription("把玩家T出当前领地!");
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        final Player player = (Player) sender;
        final ResidenceManager rmanager = plugin.getResidenceManager();
        final Language language = plugin.getLanguage();
        final PermissionGroup group = plugin.getPermissionManager().getGroup(player);

        final Player targetplayer = Bukkit.getPlayer(args[0]);
        if (targetplayer == null) {
            return;
        }
        if (!group.hasKickAccess()) {
            player.sendMessage(ChatColor.RED + language.getPhrase("NoPermission"));
            return;
        }
        final ClaimedResidence res = rmanager.getByLoc(targetplayer.getLocation());
        if (res == null) {
            player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            return;
        }
        if (res.getOwner().equals(player.getName())) {
            if (res.getPlayersInResidence().contains(targetplayer)) {
                targetplayer.teleport(res.getOutsideFreeLoc(player.getLocation()));
                targetplayer.sendMessage(ChatColor.RED + language.getPhrase("Kicked") + "!");
            }
        }
    }

}
